package handler

import (
	"context"
	"go.uber.org/zap"
	"user_srv/global"
	"user_srv/model"

	proto "user_srv/proto"
)

// AddRole 添加角色
func (h *User_srv) AddRole(ctx context.Context, req *proto.ResponseRoleInfo, rep *proto.Response) error {
	role := &model.Role{}
	if err := global.SwapTo(req, role); err != nil {
		return err
	}
	zap.S().Debug("添加角色", role)
	result := global.DB.Create(role)
	if result.Error != nil {
		return result.Error
	}
	return nil
}

// EditRole 更新角色信息
func (h *User_srv) EditRole(ctx context.Context, req *proto.ResponseRoleInfo, rep *proto.Response) error {
	role := model.Role{}
	result := global.DB.First(&role, req.Id)
	if result.Error != nil {
		zap.S().Debug(result.Error)
		return result.Error
	}
	if result.RowsAffected == 0 {
		zap.S().Errorf("ID为 [ %s ] 的角色不存在", req.Id)
		return result.Error
	}
	UpdateData := make(map[string]interface{}, 4)
	if req.Name != "" {
		UpdateData["name"] = req.Name
	}
	if req.Sorts != 0 {
		UpdateData["sorts"] = req.Sorts
	}
	if req.Pid != 0 {
		UpdateData["pid"] = req.Pid
	}
	if req.Status != 0 {
		UpdateData["status"] = req.Status
	}

	zap.S().Debug("更新参数", UpdateData)

	res := global.DB.Model(&role).Updates(UpdateData)
	//res := global.DB.Updates(UpdateData)
	zap.S().Debug("更新结果", res)
	if res.Error != nil {
		return res.Error
	}
	return nil
}

// DelRole 根据ID删除角色
func (h *User_srv) DelRole(ctx context.Context, req *proto.RequestRoleId, rep *proto.Response) error {
	role := &model.Role{}

	roleData := global.DB.First(role, req.RoleId)
	if roleData.RowsAffected == 0 {
		zap.S().Errorf("ID为 [ %s ] 的角色不存在", req.RoleId)
		return roleData.Error
	}

	result := global.DB.Delete(role)

	if result.Error != nil {
		return result.Error
	}
	return nil
}

// GetRoleInfoByRoleId 根据ID查询角色
func (h *User_srv) GetRoleInfoByRoleId(ctx context.Context, req *proto.RequestRoleId, rep *proto.ResponseRoleInfo) error {
	role := &model.Role{}
	roleData := global.DB.First(role, req.RoleId)
	if roleData.Error != nil {
		zap.S().Debug(roleData.Error)
		return roleData.Error
	}
	if roleData.RowsAffected == 0 {
		zap.S().Errorf("ID为 [ %s ] 的角色不存在", req.RoleId)
		return roleData.Error
	}
	zap.S().Debug("查询角色-查询结果", role)

	err := global.SwapTo(role, rep)
	if err != nil {
		return err
	}
	zap.S().Debug("查询角色-返回结果", rep)

	return nil
}

// GetRoleList 获取角色列表
func (h *User_srv) GetRoleList(ctx context.Context, req *proto.RequestLimitList, rep *proto.ResponseRoleList) error {
	//roles := &model.Role{}
	//roles := model.Role{}
	var roles []model.Role
	zap.S().Debug("原始结构", roles)

	//获取总数
	result := global.DB.Find(&roles)
	if result.Error != nil {
		return result.Error
	}
	rep.Total = int32(result.RowsAffected)
	rep.Page = int32(req.Page)
	rep.Limit = int32(req.Limit)
	zap.S().Debug("全量数据", roles)

	//获取查询条数

	if req.Page > 0 && req.Limit > 0 {
		global.DB.Scopes(model.Paginate(int(req.Page), int(req.Limit))).Find(&roles)
	}
	for _, role := range roles {
		Datas := &proto.ResponseRoleInfo{}
		err := global.SwapTo(role, Datas)
		if err != nil {
			return err
		}
		rep.List = append(rep.List, Datas)
	}
	return nil
}
